package br.com.sovi.gwt.client.historico.marcador;

import java.util.List;

import br.com.sovi.gwt.client.application.Application;
import br.com.sovi.gwt.client.application.ApplicationConstants;
import br.com.sovi.gwt.client.component.AsyncReturn;
import br.com.sovi.gwt.client.historico.HistoricoMarker;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.gwtext.client.util.DateUtil;

public class MarcadorInfoController {

	private static List<HistoricoMarker> markers;

	public static String getDataServidor() {
		if (getMarker().getHistoricoPoint().getData() != null) {
			return DateUtil.format(getMarker().getHistoricoPoint().getData(), "H:i:s d/m/y");
		}

		return null;
	}

	public static HistoricoMarker getNextMarker() {
		return getNextMarker(getMarker());
	}

	public static HistoricoMarker getNextMarker(HistoricoMarker marker) {
		if (getMarkers() != null) {
			int indexOf = getMarkers().indexOf(marker);
			int ultimoPonto = getMarkers().size() - 1;

			if (indexOf >= 0 && indexOf < ultimoPonto) {
				return getMarkers().get(indexOf + 1);
			}
		}
		return null;
	}

	public static HistoricoMarker getPreviousMarker() {
		return getPreviousMarker(getMarker());
	}

	public static HistoricoMarker getPreviousMarker(HistoricoMarker marker) {
		if (getMarkers() != null) {
			int indexOf = getMarkers().indexOf(marker);
			int ultimoPonto = getMarkers().size() - 1;

			if (indexOf > 0 && indexOf <= ultimoPonto) {
				return getMarkers().get(indexOf - 1);
			}
		}
		return null;
	}

	public static void getAddress(final AsyncReturn<String> asyncReturn) {
		getAddress(getMarker(), asyncReturn);
	}

	public static void getAddress(final HistoricoMarker marker, final AsyncReturn<String> asyncReturn) {
		if (marker.getJsArray() == null) {

			searchGeoCode(marker, new AsyncReturn<JsArray<Placemark>>() {
				@Override
				public void onReturn(JsArray<Placemark> t) {
					if (t == null) {
						asyncReturn.onReturn("Não foi possível capturar o endereço.");
					}
					else {
						marker.setJsArray(t);
						asyncReturn.onReturn(t.get(0).getAddress());
					}
				}
			});

		}
		else {
			asyncReturn.onReturn(MarcadorInfoController.getMarker().getJsArray().get(0).getAddress());
		}
	}

	public static void searchGeoCode(final HistoricoMarker marker, final AsyncReturn<JsArray<Placemark>> asyncReturn) {
		new Geocoder().getLocations(getMarker().getLatLng(), new LocationCallback() {
			@Override
			public void onSuccess(JsArray<Placemark> locations) {
				GWT.log("Recebendo as locations...", null);
				asyncReturn.onReturn(locations);
			}

			@Override
			public void onFailure(int statusCode) {
				GWT.log("Falha no geocoding: " + statusCode, null);
				asyncReturn.onReturn(null);
			}
		});
	}

	public static void setMarkers(List<HistoricoMarker> markers) {
		MarcadorInfoController.markers = markers;
	}

	public static List<HistoricoMarker> getMarkers() {
		return markers;
	}

	public static HistoricoMarker getMarker() {
		return (HistoricoMarker) Application.get().getAttribute(ApplicationConstants.HISTORICO_MARCADOR_SELECIONADO);
	}

	public static void setMarker(HistoricoMarker historicoMarker) {
		Application.get().addAttribute(ApplicationConstants.HISTORICO_MARCADOR_SELECIONADO, historicoMarker);
	}

}
